package system;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;


@SuppressWarnings("serial")
public class 
View 
extends JFrame
{	 private int framePositionX;
	 private int framePositionY;
	 private Rectangle frameBounds = null;	// initial size of main frame
	 private JTextArea statusField = new JTextArea(5,0);
	 
	 // the scrollpane that will hold the table of database data
	 private JScrollPane tableScrPane = new JScrollPane();
	 private JScrollPane auxiliaryPane = new JScrollPane();
	 
	 // menubar containing application functionality
	private MenuBar menu_bar = new MenuBar();

	 
	 public 
	 View()
	 {	// should call the constructor of the superclass first
		super("Allegro Music Store");
		setSize(650,690);
	
		// the content pane;
		// components will be spaced vertically 10 pixels apart
		JPanel contentPane = new JPanel(new BorderLayout(0, 10));
		setContentPane(contentPane);
	
		// leave some space around the content pane
		contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
	
		setJMenuBar(menu_bar);
		
		// the scrollpane for the status text field
		JScrollPane statusScrPane = new JScrollPane(statusField);
		statusScrPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		statusScrPane.setBorder(BorderFactory.createLoweredBevelBorder());
		
		// set status field properties
		statusField.setEditable(false);
		statusField.setLineWrap(true);
		statusField.setWrapStyleWord(true);
	
		// add the panes to the content pane
		contentPane.add(tableScrPane, BorderLayout.CENTER);
		contentPane.add(statusScrPane, BorderLayout.NORTH);
		
		contentPane.add(auxiliaryPane, BorderLayout.SOUTH);
		
		// center the main window
		Dimension screenSize = getToolkit().getScreenSize();
		frameBounds = getBounds();
		framePositionX = (screenSize.width - frameBounds.width)/2;
		framePositionY = (screenSize.height - frameBounds.height)/2;
		setLocation(framePositionX, framePositionY);
	     	
		// anonymous inner class to terminate program
		addWindowListener(new WindowAdapter() 
		{	public void windowClosing(WindowEvent e){ System.exit(0); } });	
	 }
	 
	 // called by LoginWindow when it closes successfully
	 public void 
	 registerControllers(){	menu_bar.registerControllers(this); }
	 
	 /**************************************************************************
	  * The following methods are services provided for the application 
	  * controllers.
	  *************************************************************************/
	 
	 /** Method called by controllers to orient new dialog windows. */
	 public void 
	 centerWindow(Window w)
	 {	Rectangle winBounds = w.getBounds();
		w.setLocation(
			framePositionX + (frameBounds.width - winBounds.width)/2, 
			framePositionY + (frameBounds.height - winBounds.height)/2
		);
	 }

	 /** Method called by controllers when exceptions are thrown, and by 
	  *  the dialog windows when they perform some action.
	  *  
	  * @param s The string to display in the status bar.
	  */
	 public void 
	 updateStatusBar(String s)
	 {	statusField.append(s.trim() + "\n"); 
	
		// This informs the scroll pane to update itself and its scroll bars.
		// The scroll pane does not always automatically scroll to the message that was
		// just added to the text area. This line guarantees that it does.
		statusField.revalidate();
	 }
	 
	 public void 
	 addTable(JTable data)
	 {	
		 tableScrPane.setViewportView(data);
		 auxiliaryPane.setViewportView(null);
		 setSize(650,695); // force proper repaint
	 }
	 
	 public void
	 addAuxiliaryTable(JTable data)
	 { 
		auxiliaryPane.setViewportView(data);
		setSize(650,699); // force proper repaint
	 }
}

